package exam.bak.beike;

import java.util.*;

public class P4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] params = Arrays.stream(scanner.nextLine().trim().split(" ")).mapToInt(s -> Integer.parseInt(s)).toArray();
        int n = params[0];
        int m = params[1];
        List<List<int[]>> blocksList = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            int k = Integer.parseInt(scanner.nextLine());
            List<int[]> blockList = new ArrayList<>();
            for (int j = 0; j < k; j++) {
                blockList.add(Arrays.stream(scanner.nextLine().trim().split(" ")).mapToInt(s -> Integer.parseInt(s)).toArray());
            }
            blocksList.add(blockList);
        }
        // 1.
        List<Integer> resultList = new ArrayList<>();
        bt(0, n, m, blocksList, new ArrayList<>(), resultList);
        System.out.println(resultList.get(0));
    }

    private static void bt(int pos, int n, int m, List<List<int[]>> blocksList, List<List<Integer>> savedList, List<Integer> resultList) {
        if (pos == n) {//计算结果
            Map<Integer, Integer> map = new HashMap<>();
            for (List<Integer> list : savedList) {
                for (int num : list) {
                    if (!map.containsKey(num)) {
                        map.put(num, 0);
                    }
                    map.put(num, map.get(num) + 1);
                }
            }
            int result = 0;
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                result += entry.getValue() * entry.getValue();
            }
            if (resultList.size() == 0) {
                resultList.add(0);
            }
            if (resultList.get(0) < result) {
                resultList.set(0, result);
            }
        } else {
            List<List<Integer>> lists = validPos(blocksList.get(pos));
            for (List<Integer> row : lists) {
                savedList.add(row);
                bt(pos + 1, n, m, blocksList, savedList, resultList);
                savedList.remove(savedList.size() - 1);
            }

        }
    }

    private static List<List<Integer>> validPos(List<int[]> blockList) {
        List<List<Integer>> resultList = new ArrayList<>();
        bk2(blockList, 0, new ArrayList<>(), resultList);
        return resultList;
    }

    private static void bk2(List<int[]> blockList, int pos, List<Integer> list, List<List<Integer>> resultList) {
        if (pos < blockList.size()) {
            Integer history = null;
            for (int num : blockList.get(pos)) {
                if (history != null && history == num) {
                    break;
                }
                history = num;
                list.add(num);
                bk2(blockList, pos + 1, list, resultList);
                list.remove(list.size() - 1);
            }
        } else {
            resultList.add(new ArrayList<>(list));
        }
    }

    public static void main1(String[] args) {
        List<int[]> list = new ArrayList<>();
        list.add(new int[]{1, 2});
        list.add(new int[]{3, 4});
        list.add(new int[]{5, 5});
        List<List<Integer>> resultList = new ArrayList<>();
        bk2(list, 0, new ArrayList<>(), resultList);
        System.out.println(resultList);

    }
}
